/**
 * 
 */
package com.buss.base.dao.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.base.dao.DicDataDao;
import com.buss.base.model.DicData;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;

/**
 * @author gykl
 *
 */
@Repository
public class DicDataDaoImpl implements DicDataDao {

	@Autowired
	private BaseDao<DicData> baseDao;
	
	@Override
	public DicData saveDicData(DicData dicData) {
		return baseDao.saveOrEdit(dicData);
	}

	@Override
	public void editDicData(DicData dicData) {
		baseDao.edit(dicData);
	}

	@Override
	public void removeDicData(Long[] ids) {
		String sql = "delete from DicData dicData where dicData.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public DicData loadDicData(Long id) {
		return baseDao.load(id, DicData.class);
	}

	@Override
	public DicData loadDicData(String companyId, String code) {
		String sql = "select dicData from DicData dicData where dicData.companyId = :companyId and " +
				"dicData.code = :code";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("code", code);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public ResultList<DicData> queryDicData(String companyId, Long parentId,String name,Paginalbe page,OrderBy orderBy) {
		String sql = "select dicData from DicData dicData where dicData.companyId = :companyid and " +
				"dicData.parentId = :parentId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyid", companyId);
		params.put("parentId", parentId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and dicData.name like :name ";
			params.put("name", "%"+name+"%");
		}		
		if(orderBy != null){
			sql += orderBy.aggOrder("dicData");
		}
		return baseDao.queryHsql(sql, params, page);
	}

	public Long getRowCount(String companyId,Long parentId){
		String sql = "select count(dicData) from DicData dicData where dicData.companyId = :companyId and " +
				"dicData.parentId = :parentId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("parentId", parentId);
		Long cnt = (Long)baseDao.loadSingleValue(sql, params);
		return cnt;
	}
	
}
